import aiohttp

from api.xbot.xbot_base_api import XbotBaseApi


class GroupApi(XbotBaseApi):
    # ==================== Group 群组模块 ====================
    async def group_get_chat_room_info_detail(self, wxid: str, qid: str):
        """获取群信息(带公告内容)"""
        body = {
            "Wxid": wxid,
            "QID": qid
        }
        async with aiohttp.ClientSession() as session:
            async with session.post(
                    f"{self.base_url}/Group/GetChatRoomInfoDetail",
                    json=body
            ) as response:
                return await response.json()

    async def group_get_chat_room_info(self, wxid: str, qid: str):
        """获取群详情(不带公告内容)"""
        body = {
            "Wxid": wxid,
            "QID": qid
        }
        async with aiohttp.ClientSession() as session:
            async with session.post(
                    f"{self.base_url}/Group/GetChatRoomInfo",
                    json=body
            ) as response:
                return await response.json()

    async def group_get_chat_room_member_detail(self, wxid: str, qid: str):
        """获取群成员详情"""
        body = {
            "Wxid": wxid,
            "QID": qid
        }
        async with aiohttp.ClientSession() as session:
            async with session.post(
                    f"{self.base_url}/Group/GetChatRoomMemberDetail",
                    json=body
            ) as response:
                return await response.json()

    async def group_create_chat_room(self, wxid: str, to_wxids: str, chat_room_name: str = ""):
        """创建群聊 - to_wxids多个微信ID用逗号隔开，至少三个好友微信ID以上"""
        body = {
            "Wxid": wxid,
            "ToWxids": to_wxids,
            "ChatRoomName": chat_room_name
        }
        async with aiohttp.ClientSession() as session:
            async with session.post(
                    f"{self.base_url}/Group/CreateChatRoom",
                    json=body
            ) as response:
                return await response.json()

    async def group_add_chat_room_member(self, wxid: str, qid: str, to_wxids: str):
        """增加群成员(40人以内) - to_wxids多个微信ID用逗号隔开"""
        body = {
            "Wxid": wxid,
            "QID": qid,
            "ToWxids": to_wxids
        }
        async with aiohttp.ClientSession() as session:
            async with session.post(
                    f"{self.base_url}/Group/AddChatRoomMember",
                    json=body
            ) as response:
                return await response.json()

    async def group_del_chat_room_member(self, wxid: str, qid: str, to_wxids: str):
        """删除群成员 - to_wxids多个微信ID用逗号隔开"""
        body = {
            "Wxid": wxid,
            "QID": qid,
            "ToWxids": to_wxids
        }
        async with aiohttp.ClientSession() as session:
            async with session.post(
                    f"{self.base_url}/Group/DelChatRoomMember",
                    json=body
            ) as response:
                return await response.json()

    async def group_set_chat_room_name(self, wxid: str, qid: str, content: str):
        """设置群名称"""
        body = {
            "Wxid": wxid,
            "QID": qid,
            "Content": content
        }
        async with aiohttp.ClientSession() as session:
            async with session.post(
                    f"{self.base_url}/Group/SetChatRoomName",
                    json=body
            ) as response:
                return await response.json()

    async def group_set_chat_room_announcement(self, wxid: str, qid: str, content: str):
        """设置群公告"""
        body = {
            "Wxid": wxid,
            "QID": qid,
            "Content": content
        }
        async with aiohttp.ClientSession() as session:
            async with session.post(
                    f"{self.base_url}/Group/SetChatRoomAnnouncement",
                    json=body
            ) as response:
                return await response.json()

    async def group_set_chat_room_remarks(self, wxid: str, qid: str, content: str):
        """设置群备注"""
        body = {
            "Wxid": wxid,
            "QID": qid,
            "Content": content
        }
        async with aiohttp.ClientSession() as session:
            async with session.post(
                    f"{self.base_url}/Group/SetChatRoomRemarks",
                    json=body
            ) as response:
                return await response.json()

    async def group_operate_chat_room_admin(self, wxid: str, qid: str, to_wxids: str, val: int):
        """操作群管理员 - to_wxids多个微信ID用逗号隔开，val: 1设置管理员 2取消管理员"""
        body = {
            "Wxid": wxid,
            "QID": qid,
            "ToWxids": to_wxids,
            "Val": val
        }
        async with aiohttp.ClientSession() as session:
            async with session.post(
                    f"{self.base_url}/Group/OperateChatRoomAdmin",
                    json=body
            ) as response:
                return await response.json()

    async def group_quit(self, wxid: str, qid: str):
        """退出群聊"""
        body = {
            "Wxid": wxid,
            "QID": qid
        }
        async with aiohttp.ClientSession() as session:
            async with session.post(
                    f"{self.base_url}/Group/Quit",
                    json=body
            ) as response:
                return await response.json()

    async def group_get_qr_code(self, wxid: str, qid: str):
        """获取群二维码"""
        body = {
            "Wxid": wxid,
            "QID": qid
        }
        async with aiohttp.ClientSession() as session:
            async with session.post(
                    f"{self.base_url}/Group/GetQRCode",
                    json=body
            ) as response:
                return await response.json()

    async def group_scan_into_group(self, wxid: str, url: str):
        """扫码进群"""
        body = {
            "Wxid": wxid,
            "Url": url
        }
        async with aiohttp.ClientSession() as session:
            async with session.post(
                    f"{self.base_url}/Group/ScanIntoGroup",
                    json=body
            ) as response:
                return await response.json()

    async def group_consent_to_join(self, wxid: str, url: str):
        """同意进入群聊"""
        body = {
            "Wxid": wxid,
            "Url": url
        }
        async with aiohttp.ClientSession() as session:
            async with session.post(
                    f"{self.base_url}/Group/ConsentToJoin",
                    json=body
            ) as response:
                return await response.json()